/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.rtx.ui.frame;

import com.alee.extended.image.WebDecoratedImage;
import com.alee.extended.image.WebImage;
import com.alee.extended.panel.CenterPanel;
import com.alee.extended.panel.GroupPanel;
import com.alee.extended.progress.WebProgressOverlay;
import com.alee.laf.button.WebButton;
import com.alee.laf.checkbox.WebCheckBox;
import com.alee.laf.combobox.WebComboBox;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.progressbar.WebProgressBar;
import com.alee.laf.text.WebPasswordField;
import im.rtx.base.event.EventHandler;
import im.rtx.ui.component.base.BaseFrame;
import im.rtx.ui.component.pannel.TitlePanel;
import im.rtx.utils.ImageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Project  : rtx
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2014/9/8
 * License  : Apache License 2.0
 */
@Service
@EventHandler
public class LoginFrame extends BaseFrame {
    private WebDecoratedImage avatarImage;
    private WebComboBox accountCbx;
    private WebPasswordField passwordFld;
    private WebCheckBox autoLoginCkb;
    private WebCheckBox remPwdCkb;
    private WebButton loginBtn;
    private WebProgressBar loginPb;
    @PostConstruct
    public void init(){
        TitlePanel titlePanel = getTitlePanel();
        titlePanel.setShowSkinButton(false);
        titlePanel.setShowMaximizeButton(false);

        this.initFrame();
        this.add(createMiddle(), BorderLayout.CENTER);
        this.add(createFooter(), BorderLayout.SOUTH);
        this.setSize(new Dimension(200, 180));
        this.setTitle("RTX登录");
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        //this.pack();
        //this.setVisible(true);
    }

    private Component createMiddle(){
        WebPanel webPanel = new WebPanel();
        webPanel.setLayout(new BoxLayout(webPanel, BoxLayout.Y_AXIS));
        webPanel.setMargin(5, 20, 5, 20);

        Dimension dimFld = new Dimension(50, 30);

        //头像部分
        ImageIcon avatar = getThemeService().getImageIcon("base/avatar/defaultAvatar");
        avatar = ImageUtils.getScaledInstance(avatar, 100, 100);
        avatarImage = new WebDecoratedImage(avatar);
        avatarImage.setShadeWidth(3);
        avatarImage.setRound(3);
        avatarImage.setBorderColor(Color.white);

        // 账号输入一行
        String[] items = {"rongqian.xu", "kiki", "solosky"};
        accountCbx = new WebComboBox(items);
        accountCbx.setEditable(true);
        //accountCbx.setPreferredSize(dimFld);
        accountCbx.setSize(dimFld);

        //密码
        passwordFld = new WebPasswordField();
        // 密码框小图标
        WebImage keyIcon = new WebImage(getThemeService().getImageIcon("login/keyIcon"));
        passwordFld.setTrailingComponent(keyIcon);
        //passwordFld.setPreferredSize(dimFld);
        passwordFld.setSize(dimFld);

        //自动登录
        autoLoginCkb = new WebCheckBox("自动登录");
        remPwdCkb = new WebCheckBox("记住密码");
        GroupPanel checkGroup = new GroupPanel(1, true, autoLoginCkb, remPwdCkb);

        webPanel.add(new GroupPanel(3, false, avatarImage, accountCbx, passwordFld, checkGroup));

        return webPanel;
    }

    private Component createFooter(){
        //footerPanel.setOpaque(false);
        final WebPanel footerPanel = new WebPanel();
        footerPanel.setPreferredSize(new Dimension(-1, 40));

        final WebProgressOverlay progressOverlay = new WebProgressOverlay ();

        loginBtn = new WebButton("登录");
        loginBtn.setPreferredHeight(28);
        loginBtn.setPreferredWidth(140);
        loginBtn.setMargin(5);
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean showLoad = !progressOverlay.isShowLoad ();
                progressOverlay.setShowLoad ( showLoad );
                loginBtn.setText(showLoad ? "登录中..." : "登录");
            }
        });


        progressOverlay.setComponent(loginBtn);
        progressOverlay.setConsumeEvents(false);


        footerPanel.add(new CenterPanel(progressOverlay), BorderLayout.CENTER);

        return footerPanel;
    }
}
